package cn.wang.service.impl;

import cn.wang.common.Exceptions.impl.LyException;
import cn.wang.common.enums.impl.ExceptionEnum;
import cn.wang.mapper.ICategoryMapper;
import cn.wang.pojo.Category;
import cn.wang.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class CategoryService implements ICategoryService {

    @Autowired
    private ICategoryMapper categoryMapper;

    @Override
    public List<Category> queryListByPid(Long pid) {
        Category t = new Category();
        t.setParentId(pid);
        // 通过传递对象的非空字段进行查询
        List<Category> select = categoryMapper.select(t);
        if (CollectionUtils.isEmpty(select)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOND);
        }
        return select;
    }

    @Override
    public List<Category> queryByBid(Long bid) {
        return categoryMapper.selectByBid(bid);
    }

    @Override
    public List<Category> queryByCids(Long... ids) {
        return categoryMapper.selectByIdList(Arrays.asList(ids));
    }

    /**
     * 根据商品分类查询商品
     * @param cid
     * @return
     */
    @Override
    public Category queryByCid(Long cid) {
        return categoryMapper.selectByPrimaryKey(cid);
    }


}
